package com.huawei.chengdu.meetingdemo.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.device.idea.meeting.Addressbook.model.AddrBookNetUserInfo;
import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.presenter.ConfSdkPresenter;

import java.util.ArrayList;
import java.util.List;

public class NetContactListAdapter extends RecyclerView.Adapter<NetContactListAdapter.CompanyContactViewHolder> {
    private final List<AddrBookNetUserInfo> contactList = new ArrayList<>();
    public List<AddrBookNetUserInfo> selectedContactList = new ArrayList<>();

    @NonNull
    @Override
    public CompanyContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_contact_net, parent, false);
        return new CompanyContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CompanyContactViewHolder holder, int position) {
        AddrBookNetUserInfo addrBookNetUserInfo = contactList.get(position);
        holder.contactName.setText(addrBookNetUserInfo.getName());
        holder.contactNumber.setText(addrBookNetUserInfo.getLineInfo().getSiteNumber());
        holder.contactCall.setOnClickListener(v -> ConfSdkPresenter.companyAddressBookCall(addrBookNetUserInfo));
        holder.contactSelect.setOnCheckedChangeListener((buttonView, isChecked) ->
                handleContactSelect(isChecked, addrBookNetUserInfo));
    }

    public void updateContactList(List<AddrBookNetUserInfo> newContactList) {
        contactList.clear();
        contactList.addAll(newContactList);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }

    private void handleContactSelect(boolean isSelect, AddrBookNetUserInfo addrBookNetUserInfo) {
        if(isSelect) {
            selectedContactList.add(addrBookNetUserInfo);
        } else {
            selectedContactList.remove(addrBookNetUserInfo);
        }
    }

    public static class CompanyContactViewHolder extends RecyclerView.ViewHolder {
        private final TextView contactName;
        private final TextView contactNumber;
        private final View contactCall;
        private final CheckBox contactSelect;

        public CompanyContactViewHolder(@NonNull View itemView) {
            super(itemView);
            contactName = itemView.findViewById(R.id.company_contact_name);
            contactNumber = itemView.findViewById(R.id.company_contact_number);
            contactCall = itemView.findViewById(R.id.company_contact_call);
            contactSelect = itemView.findViewById(R.id.net_contact_select);
        }
    }

}
